<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class WxIcon extends Model
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            // 在模型创建之前触发的操作
        });

        static::created(function ($model) {

        });

        static::updating(function ($model) {
            // 在模型更新之前触发的操作
            $model->original = $model->getOriginal();
        });

        static::updated(function ($model) {
            $original = $model->original;
            $changes = $model->getChanges();

            // 对比更新前后的字段
            foreach ($changes as $key => $value) {
                if($key == 'img'){
                    $originalValue = $original[$key] ?? 0;
                    WxIcon::where('id', $model->id)->update([
                        'img_width' => null,
                        'img_height' => null
                    ]);
                }
            }
        });
        static::deleted(function ($model) {

        });
        // 其他的模型事件监听...
    }

    protected $table = 'wx_icons';

}
